package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.BreakfastDAO;
import com.wanmait.hotelManage.vo.Breakfast;

/**
 * Servlet implementation class BreakfastServlet
 */
@WebServlet("/manage/BreakfastServlet")
public class BreakfastServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BreakfastServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String action=request.getParameter("action");
	if("init".equals(action)) {
		this.init(request, response);
	}else if("update".equals(action)) {
		this.update(request, response);
	}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	/**
	 * 更改价钱
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		String price=request.getParameter("price");
		BreakfastDAO.getBreakfastdao().update(id,Double.parseDouble(price));
		response.sendRedirect(request.getContextPath()+"/manage/BreakfastServlet?action=init");
	}
	/**
	 * 初始化
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	Breakfast bf=BreakfastDAO.getBreakfastdao().findall();
	request.setAttribute("bf", bf);
	request.getRequestDispatcher("/manage/breakfast/update.jsp").forward(request, response);
	}
}
